Code hoofdstuk5

terug

5.1

float a=.6,b=.4,c=a,d=-b;
float x=1,y=0,cycle=0.0;

void setup(){
randomSeed(0);
size(500, 450);
background(255);
frameRate(30);
}

void draw()
{
translate(250,300);
float x1,y1;
scale(1, -1); 
for(int k=0;k<300;k++)
{
if(random(1)<.5){
x1=a*x-b*y-1+a;
y1=b*x+a*y+b;
}
else{
x1=c*x-d*y+1-c;
y1=d*x+c*y-d;
}
x=x1;y=y1;
point(140*x,140*y);
k=k+1;
}
}
void mousePressed(){  //click to freeze or restart
cycle=cycle+1.0;
if(cycle%2==1){noLoop();}  //cycle%2 calculates the remainder of the division by 2
else{loop();}
}

terug

5.2

size(400, 300);
background(255);
stroke(0);
translate(240,100);
randomSeed(0);
float a=.6,b=.45,c=.5,d=0;
float DET1=a*a+b*b, DET2=c*c+d*d;
float Q=DET1/(DET1+DET2);
float x=1,y=0,x1,y1;
for(int k=0;k<50000;k++){
float r=random(1);
if(r<Q){
x1=a*x-b*y-1+a;
y1=b*x+a*y+b;
}
else{
x1=c*x-d*y+1-c;
y1=d*x+c*y-d;
}
x=x1;y=y1;
point(150*x,150*y);
k=k+1;
}

terug

5.4

float a=.5,b=.5,c=.6667,d=0;
float DET1=a*a+b*b, DET2=c*c+d*d;
float Q=DET1/(DET1+DET2);
float x=1,y=0,x1,y1;

void setup(){
size(480, 400);
background(255);
stroke(0);
randomSeed(0);
}

void draw(){
translate(240,180);
for(int k=0;k<100;k++){
float r=random(1);
if(r<Q){
x1=a*x+b*y-1+a;
y1=b*x-a*y+b;
}
else{
x1=c*x+d*y+1-c;
y1=d*x-c*y-d;
}
x=x1;y=y1;
point(200*x,200*y);
k=k+1;
}
}

void mousePressed() {
loop();
}

void mouseReleased(){
noLoop();
}

terug

5.6

//Sierpinski triangle
/*from H.A. Lauwerier Graphics&Fractals, 1994 chapter 5 FRACMC5 translated into
Processing by J.G.van Unnik, 2010*/

int t;
int[] colour={color(0,0,0), color(0,0,100), color(0,0,200),
color(0,0,255),color(100,0,255), color(200,0,255),
color(255,0,0),color(255,0,100),
color(255,0,200),color(255,0,255), color(255,100,0), color(255,200,0),
color(255,255,0), color(255,255,100),color(255,255,200),
color(255,255,255)};
//defines array of 16 colour numbers

void setup(){
size(400, 400);
background(0);
}
void draw(){
float E[]=new float[3];
float F[]=new float[3];
float C=.65; //contraction factor
float A=.5,B=sqrt(3)/2,X=1,Y=0;
E[0]=1-C;F[0]=0;E[1]=A*(C-1);F[1]=-B*(C-1);
E[2]=A*(C-1);F[2]=B*(C-1);

for (int k=0;k<200;k++){
int L=int(3*random(1));
X=C*X+E[L];Y=C*Y+F[L];
int j=get(standardcoordinates(X),standardcoordinates(Y));
for (int i = 1; i < 15; i = i+1) {
if (colour[i]>j){
set(standardcoordinates(X),standardcoordinates(Y),colour[i]);
i=15; //exit for-condition
}
}

}
}
int standardcoordinates(float coord){
//function that converts numbers into screen coordinates
int scalef=200, offset=200; //corresponds to BASIC window(-2,-2)-(2,2)
int stcoord=int(coord*scalef+offset);
return stcoord;
}
void keyPressed(){ //press a key to freeze or restart
t=t+1;
if (t%2==1){noLoop();}
else{loop();}
}

terug